package cn.hxsteel.storage.module.sum;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hxsteel.model.bo.ViewBO;
import cn.hxsteel.storage.core.base.BaseController;
import cn.hxsteel.storage.model.vo.SelectOptionVO;

/**
 * 统计库存的控制器
 */
@Controller
public class SumController extends BaseController
{
    @Autowired
    SumService _sumService;
    
    /**
     * 库存统计的主视图
     */
    @RequestMapping( "/depotCountManage/view" )
    public ModelAndView depotCountManageView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	String[] uriArray = new String[] { "/sumTotalDepot/view", "/sumDayDepot/view" };
	String viewName = getUserPermitViewName( uriArray, request );
	res.setViewName( viewName );
	return res;
    }
    
    /* 查询总库存
    --------------------------------------------------*/
    
    /**
     * 总库存的主视图
     */
    @RequestMapping( "/sumTotalDepot/view" )
    public ModelAndView sumTotalDepotView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/sum/sumTotalDepot/search" );
	res.addAllObjects( _sumService.searchSumDepotViewBO( getUserSessionVO( request ) ) );
	return res;
    }
    
    /**
     * 总库存的查询数据
     * 
     * @param sumDate 统计日期
     * @param uid 创始人标识
     * @param companyID 公司标识
     * @param depotID 仓库标识
     * @return
     */
    @RequestMapping( "/sumTotalDepot/view/do" )
    @ResponseBody
    public ViewBO searchSumTotalDepotData( String sumDate, Integer uid, Integer companyID, Integer depotID, Integer pageNo, Integer pageSize,
	    HttpServletRequest request )
    {
	ViewBO res = new ViewBO();
	uid = Math.max( uid, getUserSessionVO( request ).getUid() );
	res.putData( _sumService.searchSumTotalDepotData( pageNo, pageSize, sumDate, uid, companyID, depotID ) );
	res.putPager( _sumService.createPagerData() );
	return res;
    }
    
    /* 上：统计总库存；下：统计日库存
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    /**
     * 日库存的主视图
     */
    @RequestMapping( "/sumDayDepot/view" )
    public ModelAndView sumDayDepotView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/sum/sumDayDepot/search" );
	res.addAllObjects( _sumService.searchSumDepotViewBO( getUserSessionVO( request ) ) );
	return res;
    }
    
    /**
     * 日库存的查询数据
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param uid 创始人标识
     * @param companyID 公司标识
     * @param depotID 仓库标识
     * @return
     */
    @RequestMapping( "/sumDayDepot/view/do" )
    @ResponseBody
    public ViewBO searchSumDayDepotData( String startDate, String endDate, Integer uid, Integer companyID, Integer depotID, Integer pageNo, Integer pageSize,
	    HttpServletRequest request )
    {
	ViewBO res = new ViewBO();
	uid = Math.max( uid, getUserSessionVO( request ).getUid() );
	res.putData( _sumService.searchSumDayDepotData( pageNo, pageSize, startDate, endDate, uid, companyID, depotID ) );
	res.putPager( _sumService.createPagerData() );
	return res;
    }
    
    /* 公共部分
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    /**
     * 查询公司列表
     * 
     * @param uid 创始人标识
     * @param name 公司名称
     */
    @RequestMapping( "/sumDepotManage/companyList/do" )
    @ResponseBody
    public List< SelectOptionVO > selCompanyList( int uid, String name )
    {
	return _sumService.selCompanyList( uid, name );
    }
}